package com.jspxcms.core.service.impl;

import com.jspxcms.common.util.ScoreUtil;
import com.jspxcms.core.domain.UserDetail;
import com.jspxcms.core.service.ScoreService;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2014/11/6.
 */
@Service
public class ScoreServiceImpl implements ScoreService {



    public UserDetail updateScore(UserDetail detail, int score) {

        if ((detail.getScore() + score) > 0) {
            detail.setScore(detail.getScore() + score);
        } else {
            return detail;
        }
        if (detail.getScore() == null) {
            detail.setScoreLevel(0);
            return detail;
        }
        for (int i = ScoreUtil.scores.length - 1; i >= 0; i--) {
            if (detail.getScore() > ScoreUtil.scores[i]) {
                detail.setScoreLevel(Math.max(i, detail.getScoreLevel() == null ? 0 : detail.getScoreLevel()));
                //userDetailService.update(detail);
                return detail;
            }
        }
        return null;
    }

    @Override
    public UserDetail updateScore(UserDetail detail, String scoreType) {
        String tmp = ScoreUtil.property.getProperty(scoreType);
        if (tmp == null || tmp.length() == 0) {
            return detail;
        }
        int score = Integer.parseInt(tmp);
        if (scoreType.equals("score_favorite")) {
            detail.setScoreFavorite(detail.getScoreFavorite() + score);
        }
        if (scoreType.equals("score_study_online")) {
            detail.setScoreStudy(detail.getScoreStudy() + score);
        }
        return updateScore(detail, score);
    }



}


